package com.manage.controller;

import com.beans.*;
import com.beans.page.PageList;
import com.commons.util.Constants;
import com.manage.util.QiNiuUtil;
import com.manage.util.UserUtils;
import com.services.ContractService;
import com.services.NeighborhoodService;
import com.services.PhotoService;
import com.services.SysUserService;
import com.services.impl.PhotoServiceImpl;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author jiangwei
 * @since 18/2/27
 */
@Controller
@RequestMapping("contract")
public class ContractController extends BaseController{

    @Autowired
    private ContractService contractService;
    @Autowired
    private NeighborhoodService neighborhoodService;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private PhotoService photoService;

    /**
     * 查询列表
     *
     * @param contract
     * @return
     */
    @RequiresPermissions("contract:view")
    @RequestMapping(value = {"", "/", "list"})
    public String list(Contract contract, Long relationId, HttpServletRequest request) {

        contract.setCreateBy(UserUtils.getUser().getUserId());

        request.setAttribute("contract", contract);
        return "contract/list";
    }

    /**
     * 异步获取list资源
     *
     * @param pageList
     * @param contract
     * @return
     */
    @RequiresPermissions("contract:view")
    @RequestMapping("asynList")
    @ResponseBody
    public PageList asynList(PageList pageList, Contract contract) {
        contract.setCreateBy(UserUtils.getUser().getUserId());
        return contractService.queryPage(pageList, contract);
    }

    /**
     * 新增修改
     *
     * @param contractId
     * @return
     */
    @RequiresPermissions("contract:edit")
    @RequestMapping(value = {"add", "edit"}, method = RequestMethod.GET)
    public String edit(Long contractId, Model model) {
        Contract contract = new Contract();

        List<Photo> photoList = new ArrayList<Photo>();//合同附件图片

        if (contractId != null) {
            contract = contractService.queryById(contractId);

            Photo photo = new Photo();
            photo.setRelationId(contractId);
            photo.setRelationType(Constants.fourLong);//合同附件图片

            photoList = photoService.queryList(photo);
        }

        List<Neighborhood> neighborhoodList = neighborhoodService.queryList(new Neighborhood());

        List users = sysUserService.queryList(new SysUser());

        model.addAttribute("neighborhoodList", neighborhoodList);
        model.addAttribute("contract",contract);

        model.addAttribute("storeUrl", UserUtils.storeUrl);

        model.addAttribute("photoList", photoList);

        model.addAttribute("users", users);
        return "contract/edit";
    }

    @RequiresPermissions("contract:edit")
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public String save(Contract contract, HttpServletRequest request,
                       @RequestParam(value = "imgFileIds1", required = false) String[] imgFileIds,
                       @RequestParam(value = "imgFileDes1", required = false) String[] imgFileDes){
        int result = -1;
        contract.setCreateBy(UserUtils.getUser().getUserId());

        if(contract.getHouseId() !=null){
            Neighborhood neighborhood = neighborhoodService.queryById(contract.getHouseId());
            contract.setNbhName(neighborhood.getNbhName());
        }
        if (contract.getContractId() == null) {
            result = contractService.add(contract);
        } else {
            result = contractService.update(contract);
        }

        if (result > 0) {
            Photo photo = new Photo();
            //
            photo.setCreateBy(UserUtils.getUser().getUserId());
            photo.setUpdateBy(UserUtils.getUser().getUserId());
            photo.setRelationType(Constants.fourLong);//合同

            //删除和更新图片
            PhotoServiceImpl psi = new PhotoServiceImpl();
            psi.contract(photoService, contract.getContractId(), photo, imgFileIds, imgFileDes);
            QiNiuUtil.handleQUrl(imgFileIds);
        }

            return "redirect:list";
    }

    @RequiresPermissions("contract:delete")
    @RequestMapping("delete")
    @ResponseBody
    public Object delete(Long contractId) {
        Contract contract = new Contract();
        contract.setContractId(contractId);
        contract.setUpdateBy(UserUtils.getUser().getUserId());

        int result = contractService.delete(contract);

        //TODO  成功失败结果页面
        return result;
    }

    @RequestMapping("detailOfIndex")
    public String detailOfIndex(@RequestParam(value = "contractId", required = false) Long contractId,
                                Model model){
        Contract contract = new Contract();

        List<Photo> photoList = new ArrayList<Photo>();//合同附件图片

        if(contractId != null){
            contract = contractService.queryById(contractId);

            Photo photo = new Photo();
            photo.setRelationId(contractId);
            photo.setRelationType(Constants.fourLong);//合同附件图片

            photoList = photoService.queryList(photo);
        }
        model.addAttribute("contract", contract);
        model.addAttribute("storeUrl", UserUtils.storeUrl);
        model.addAttribute("photoList", photoList);

        return "contract/detail";
    }
}
